package com.cysion.study.ui.fragment

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.PendingOkAdapter
import com.cysion.study.entity.ApprovalEntity
import com.cysion.study.presenter.PendingPresenter
import com.cysion.study.ui.iview.PendingView
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_pending.*

//抄送
class PendingCopyFragment : BaseFragment(), PendingView {


    //绑定presenter
    private val presenter by lazy {
        PendingPresenter().apply {
            attach(this@PendingCopyFragment)
        }
    }

    private var mdatalist: MutableList<ApprovalEntity> = mutableListOf()
    private lateinit var pendingAdapter: PendingOkAdapter
    private var curPage = 1
    private var category = 0//0.待我审核 1.抄送我的
    override fun getLayoutId(): Int = R.layout.fragment_pending

    override fun initView() {
        initRefreshLayout()
        initRecyclerView()
    }

    override fun lazyLoad() {
        super.lazyLoad()
        curPage = 1
        //category  0.待我审核 1.抄送我的
        presenter.getList(curPage, 1)
    }


    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            //category  0.待我审核 1.抄送我的
            presenter.getList(curPage, 1)
            smartLayout.setEnableLoadMore(true)
            fl_load_state.visibility = View.GONE
        }
        smartLayout.setOnLoadMoreListener {
            fl_load_state.visibility = View.GONE
            presenter.getList(curPage, 1)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        pendingAdapter = PendingOkAdapter(mdatalist, context)
        rv_list.adapter = pendingAdapter
        rv_list.layoutManager = LinearLayoutManager(context)



    }

    override fun onGetPendingList(pendingList: MutableList<ApprovalEntity>) {
        if (curPage == 1) {
            mdatalist.clear()
        }

        val index = mdatalist.size

        /*if (index == 0) {
            pendingAdapter.notifyDataSetChanged()
        } else {
            pendingAdapter.notifyItemRangeChanged(index, 10)
        }*/
        if (pendingList.size <= 0) {
            multiView.showEmpty()
        } else {
            curPage++
            mdatalist.addAll(pendingList)
            pendingAdapter.notifyDataSetChanged()
            multiView.showContent()
        }
    }

    override fun loading() {
        Alert.loading(context)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)
    }

    override fun closeMvp() {
        presenter.detach()
    }

}
